﻿CREATE PROCEDURE [dbo].[yaf_group_save](
                @GroupID      INT,
                @BoardID      INT,
                @Name         NVARCHAR(50),
                @IsAdmin      BIT,
                @IsGuest      BIT,
                @IsStart      BIT,
                @IsModerator  BIT,
                @AccessMaskID INT  = NULL)
AS
    BEGIN
        DECLARE  @Flags INT
        SET @Flags = 0
        IF @IsAdmin <> 0
        SET @Flags = @Flags | 1
        IF @IsGuest <> 0
        SET @Flags = @Flags | 2
        IF @IsStart <> 0
        SET @Flags = @Flags | 4
        IF @IsModerator <> 0
        SET @Flags = @Flags | 8
        IF @GroupID > 0
        BEGIN
            UPDATE yaf_Group
            SET    Name = @Name,
                   Flags = @Flags
            WHERE  GroupID = @GroupID
        END
        ELSE
        BEGIN
            INSERT INTO yaf_Group
                       (Name,
                        BoardID,
                        Flags)
            VALUES     (@Name,
                        @BoardID,
                        @Flags);
            SET @GroupID = Scope_identity()
            INSERT INTO yaf_ForumAccess
                       (GroupID,
                        ForumID,
                        AccessMaskID)
            SELECT @GroupID,
                   a.ForumID,
                   @AccessMaskID
            FROM   yaf_Forum a
                   JOIN yaf_Category b
                     ON b.CategoryID = a.CategoryID
            WHERE  b.BoardID = @BoardID
        END
        SELECT GroupID = @GroupID
    END


